﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Config;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Config;

namespace AMM.UserControls {
    public partial class VendorEditControl : BaseEditControl {

        private FlexBindingSource<Vendor> _fbs;

        public override object BaseEditModel {
            get { return this._fbs.CurrentItem; }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<Vendor>(); }
                this._fbs.CurrentItem = (Vendor)value;
            }
        }

        public VendorEditControl() {
            this.InitializeComponent();            
        }

        private void VendorEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<Vendor>(); }
            this.BindBasics(this.cmbCurrency, BasicsType.Currency);
            this.BindBasics(this.cmbStatus, BasicsType.VendorStatus);
            this.BindBasics(this.cmbType, BasicsType.VendorType);
            this.BindUser();
            this.CreateFormBindings();
            this.BindValidation();
        }

        private void BindUser() {
            UserProcessor userProcessor = new UserProcessor();
            List<UserViewModel> userList = userProcessor
                .FetchUserViewModelList().Where(x => x.IsEnabled).ToList();

            this.ddlPurchaser.DisplayMemberEx = "Name";
            this.ddlPurchaser.ValueMemberEx = "Key";
            this.ddlPurchaser.HeightEx = 300;
            this.ddlPurchaser.WidthEx = 400;
            this.ddlPurchaser.PageSizeEx = 20;
            this.ddlPurchaser.DataBind(new UserViewModel(), x => new { x.Name, x.Description }, userList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.tbName, x => x.Text, y => y.Name);
            this._fbs.CreateBinding(this.tbAddress, x => x.Text, y => y.Address);
            this._fbs.CreateBinding(this.tbNameExtend, x => x.Text, y => y.NameExtend);
            this._fbs.CreateBinding(this.tbLinkman, x => x.Text, y => y.Linkman);
            this._fbs.CreateBinding(this.tbPhone, x => x.Text, y => y.Phone);
            this._fbs.CreateBinding(this.tbFax, x => x.Text, y => y.Fax);
            this._fbs.CreateBinding(this.tbEmail, x => x.Text, y => y.Email);
            this._fbs.CreateBinding(this.tbRemarks, x => x.Text, y => y.Remarks);
            this._fbs.CreateBinding(this.cmbCurrency, x => x.SelectedValue, y => y.PayCurrencyId);
            this._fbs.CreateBinding(this.cmbStatus, x => x.SelectedValue, y => y.VendorStatus);
            this._fbs.CreateBinding(this.cmbType, x => x.SelectedValue, y => y.VendorType);
            this._fbs.CreateBinding(this.ddlPurchaser, x => x.SelectedValueEx, y => y.UserId);
            this._fbs.CreateBinding(this.cbIsEnabled, x => x.Checked, y => y.IsEnabled);
            this._fbs.CreateBinding(this.tbInvoiceRate, x => x.Text, y => y.InvoiceRate, z => !z.HasValue ? string.Empty : z.Value.ToString("G0"));
            this._fbs.CreateBinding(this.tbIncreaseRate, x => x.Text, y => y.IncreaseRate, z => !z.HasValue ? string.Empty : z.Value.ToString("G0"));
            this._fbs.CreateBinding(this.tbPayCondition, x => x.Text, y => y.PayCondition);
            this._fbs.CreateBinding(this.attachment, x => x.Text, y => y.AttachmentId, DataSourceUpdateMode.OnPropertyChanged);
            this._fbs.CreateBinding(this.tbAccountNumber, x => x.Text, y => y.AccountNumber);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.tbName, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbName, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbNameExtend, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbNameExtend, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbAddress, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbAddress, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbCurrency, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbCurrency, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbStatus, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbStatus, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbType, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlPurchaser, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlPurchaser, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbLinkman, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbLinkman, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbPhone, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbPhone, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbPhone, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPhone, "电话只能由'-'和数字组成");

            this.ValidatorForEdit.SetType(this.tbInvoiceRate, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbInvoiceRate, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbInvoiceRate, "发票税率只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbIncreaseRate, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbIncreaseRate, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbIncreaseRate, "开票加价率只能由数字组成");
        }
    }
}
